package com.easyfun.healthmagicbox.comm;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.content.Context;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.PersonProfileJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.PersonProfile;
import com.easyfun.healthmagicbox.sync.CommCallback;

public class PersonProfileUploadTask extends BaseRedoTask implements Runnable,DatabaseAccess {

	String mpersonalID;
	
	public PersonProfileUploadTask(Context context,CommCallback callback)
	{
		super(context,callback);
	}
	
	public PersonProfileUploadTask(Context context,String persionID,CommCallback callback)
	{

		super(context,callback);
		mpersonalID = persionID;
	}

	@Override
	public void run() {
		try {
			List<PersonProfile> pps = getHelper(mcontext).getPersonProfileDao()
					.queryForEq("personID", mpersonalID);
			if (pps.size() == 1) {
				PersonProfile pp = pps.get(0);
				PersonProfileJsonWrapper wrapper = new PersonProfileJsonWrapper(
						pp);

				Map<String, String> parmas = new HashMap<String, String>();
			     parmas.put("jsonstring", wrapper.serialize());
				String result = super.uploadByHTTP(parmas,ConstantData.PersonProfilePOSTURI);
				
				if (ServerResponseCode.parseOf(result)==ServerResponseCode.SUCCESS)
				{
					ccb.setCommResult(this, 
							"", ServerResponseCode.SUCCESS);
				}else
				{
					ccb.setCommResult(this, 
							"", ServerResponseCode.FAILED);
				}
			}
		} catch (Exception e) {

			ccb.setCommResult(this, 
					mpersonalID, ServerResponseCode.FAILED);
			e.printStackTrace();
		} 
		
	}
	
	@Override
	public DatabaseHelper getHelper(Context context) {

		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}
	@Override
	public String serializeParam()
	{
		return mpersonalID;
	}
	
	@Override
	public void deserializeParam(String serialstring)
	{
		mpersonalID = serialstring;
	}
	
}
